import React, { useRef, useState } from "react";
import { Space, Tabs } from "antd";
import CouponAllotTable from "./CouponAllotTable";
import { useComponentDidMount } from "@/hooks";

// 切换tab需要刷新的tab
const tabChangeShouldUpdateTab = ['all', 'claimedNum'];


const CouponAllot = () => {
  const allRef = useRef();
  const waitClaimNumRef = useRef();
  const claimedNumRef = useRef();
  const [activeKey, setActiveKey] = useState<string>('all');
  const [tabNum, setTabNum] = useState<any>({all: 0, authed: 0, waitOrder: 0, ordered: 0, canceled: 0});

  const fetchTabNum = async () => {
    // const res = await fetchClueTabNum();
    const res = {};
    setTabNum({
      all: res?.total || 0,
      waitClaimNum: res?.waitClaimNum || 0,
      claimedNum: res?.claimedNum || 0,
    });
  }


  const tabChange = (key: string) => {
    setActiveKey(key);  
  }

  const refreshNum = () => {
    fetchTabNum();
  }

  const search = () => {
    setTimeout(() => {
      const refObj = tabList.find(v => v.key === activeKey)?.comRef;
      refObj?.current?.refreshTable();
    }, 0)
  }

  const childrenProps: {refreshNum: any; activeKey: string;} = {
    refreshNum,
    activeKey,
  }




  const tabList: Array<any> = [
    {
      title: '全部',
      key: 'all',
      label: tabNum?.all > 0 ? `全部 ( ${ tabNum?.all} )` : '全部',
      comRef: allRef,
      children: <CouponAllotTable {...childrenProps} tabKey="all" tabChangeShouldUpdateTab={tabChangeShouldUpdateTab} ref={allRef} />,
    },
    {
      title: '待审核',
      key: 'todo',
      label: tabNum?.waitClaimNum > 0 ? `待审核 ( ${ tabNum?.waitClaimNum} )` : '待审核',
      comRef: waitClaimNumRef,
      children: <CouponAllotTable {...childrenProps} tabKey="waitClaimNum" tabChangeShouldUpdateTab={tabChangeShouldUpdateTab} ref={waitClaimNumRef} />,
    },
    {
      title: '审核通过',
      key: 'done',
      label: tabNum?.claimedNum > 0 ? `审核通过 ( ${ tabNum?.claimedNum} )` : '审核通过',
      comRef: claimedNumRef,
      children: <CouponAllotTable {...childrenProps} tabKey="claimedNum" tabChangeShouldUpdateTab={tabChangeShouldUpdateTab} ref={claimedNumRef} />,
    },
    {
      title: '审核不通过',
      key: 'fail',
      label: tabNum?.claimedNum > 0 ? `审核不通过 ( ${ tabNum?.claimedNum} )` : '审核不通过',
      comRef: claimedNumRef,
      children: <CouponAllotTable {...childrenProps} tabKey="claimedNum" tabChangeShouldUpdateTab={tabChangeShouldUpdateTab} ref={claimedNumRef} />,
    }
  ];


  useComponentDidMount(() => {
    fetchTabNum();
  });
  
  return (
    <>
      <Tabs
        activeKey={activeKey}
        onChange={tabChange}
        tabBarExtraContent={{
          right: (
            <Space>
             {/* <ThrottleButton onClick={search} title="刷新列表" />
              <Button type="primary" onClick={handleImport}><PlusOutlined/>批量导入线索</Button> */}
            </Space>
          )
        }} 
        items={tabList}
      />
    </>
  );
};


export default CouponAllot;